home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.02 Feb 91 / XWindoid / xwindoid.c
Encoding:
C/C++ Source or Header  |  1990-12-11  |  6.7 KB  |  355 lines  |  [TEXT/KAHL]

  1. /********************************/
  2. /* File: xwindoid.c                */
  3. /*                                */
  4. /* A sample XCMD for Hypercard    */
  5. /* 2.0 that displays and handles*/
  6. /* an external window.            */
  7. /*                                */
  8. /* Well-behaved XCMDs for HC2.0    */
  9. /* will respond to the ! and ?    */
  10. /* requests by returning version*/
  11. /* and usage information         */
  12. /* respectively.                */
  13. /*                                */
  14. /* ----------------------------    */
  15. /* ©1990, Donald Koscheka        */
  16. /* All Rights Reserved            */
  17. /********************************/
  18.  
  19. /*
  20.     Project:
  21.  
  22.         ANSI-A4    -- standard "C" libraries assembled
  23.                     off of register A4
  24.                     
  25.         MacTraps
  26.         HyperXLib-- Hypercard 2.0 callback library available from 
  27.                 Apple Computer, Inc.
  28.                 
  29.         xwindoid.c (contents of listing 1)
  30.  
  31.     Set Project Type:
  32.         Type == XCMD | XFCN
  33.         Name == xwindoid
  34.         id     == -32768..32767
  35.         
  36.     Usage
  37.     
  38.         xwindoid "?"
  39.         xwindoid "!"
  40.         put the result
  41.         
  42.         OR
  43.             
  44.         Put xwindoid( "?" )
  45.         Put xwindoid( "!" )
  46. */
  47.  
  48. #include    <SetUpA4.h>
  49. #include    <string.h>
  50. #include    <HyperXCMD.h>
  51.  
  52. #ifndef    NIL
  53.     #define NIL    (void *)0L
  54. #endif
  55.  
  56. #define        ETX                0x03    
  57. #define        BS                0x08        
  58. #define        TAB                0x09
  59. #define        LF                0x0A
  60. #define        NEWLINE            0x0D
  61. #define        CR                0x0D
  62. #define        LEFT_ARROW        0x1C
  63. #define        RIGHT_ARROW        0x1D
  64. #define        UP_ARROW        0x1E
  65. #define        DOWN_ARROW        0x1F
  66.  
  67. /* Multifinder events and masks    */
  68. #ifndef    MouseMovedEvt
  69.     #define        MouseMovedEvt        0xFA    
  70. #endif
  71.  
  72. #ifndef    SuspendResumeEvt
  73.     #define        SuspendResumeEvt    0x01
  74. #endif
  75.  
  76. #ifndef    ResumeEvtMask
  77.     #define        ResumeEvtMask        0x01
  78. #endif
  79.  
  80. #ifndef    ConvertScrapMask
  81.     #define        ConvertScrapMask    0x02    
  82. #endif
  83.  
  84.  
  85.  
  86. pascal void HandleHCEvent( XCmdPtr    pp );
  87.  
  88.  
  89. Handle    strToParam( str )
  90.     char    *str;
  91. /***************************
  92. * Given a pointer to a string,
  93. * copy that string into a handle
  94. * and return the handle.
  95. *
  96. * The input and output strings
  97. * are both null-terminated
  98. *
  99. ***************************/
  100. {
  101.     Handle    outH = NIL;
  102.     long    len    = 0;
  103.     
  104.     len = strlen( str );
  105.     if( len )
  106.         if( outH = NewHandle( len ) )
  107.             BlockMove( str, *outH, len + 1 );
  108.     
  109.     return( outH );
  110. }
  111.  
  112.  
  113. pascal void main( pp )
  114.     XCmdPtr    pp;
  115. {
  116.     Handle        answer    = NIL;
  117.     char        *str;
  118.     long        len;
  119.     WindowPtr    wind;
  120.     TEHandle    hTE;
  121.     Rect        bounds;
  122.     
  123.     pp->returnValue = NIL;
  124.  
  125.     if( pp->paramCount < 0 ){
  126.         HandleHCEvent( pp );
  127.         return;
  128.     }
  129.     
  130.     if (pp->paramCount == 1){
  131.         if ( **(pp->params[0]) == '!' ){
  132.             pp->returnValue = strToParam("\pxwindoid XCMD, version 1.0, ©1990, Donald Koscheka");
  133.             return;
  134.         }
  135.         
  136.         if ( **(pp->params[0]) == '?' ){
  137.             pp->returnValue = strToParam("\pSimple xwindoid handler.");
  138.             return;
  139.         }
  140.     }
  141.     
  142.     /* now open a window to play with */
  143.     bounds.top = bounds.left = 0;
  144.     bounds.bottom = 320;
  145.     bounds.right = 500;
  146.     wind = NEWXWINDOW( pp, &bounds, "\pSample Window", FALSE, documentProc, FALSE, FALSE);
  147.     CenterWindow( wind );
  148.     
  149. }
  150.  
  151.  
  152.  
  153. pascal void HandleHCEvent( XCmdPtr    pp )
  154. /**********************************
  155. * Handle events in our xWindows    
  156. * returns true if the event was handled ok
  157. *
  158. **********************************/
  159. {
  160.     XWEventInfoPtr    ip    = pp->params[0];
  161.     WindowPtr        whichWindow;
  162.     short            windoPart;
  163.     TEHandle        hTE;
  164.     Rect            bounds;
  165.     Point            hit;
  166.     char            theKey;
  167.     GrafPtr            oldPort;
  168.     short            extend;
  169.     
  170.     pp->passFlag = TRUE;    /* seems to be more often the case */
  171.     
  172.     switch( ip->event.what ){
  173.     case mouseDown:
  174.         windoPart = FindWindow( ip->event.where, &whichWindow );
  175.             
  176.         if( whichWindow )
  177.             switch ( windoPart ){
  178.                 case inGoAway:
  179.                     if (TrackGoAway( whichWindow, ip->event.where) ){
  180.                         CLOSEXWINDOW( pp,whichWindow );
  181.                         pp->passFlag = FALSE;
  182.                     }
  183.                     break;
  184.  
  185.                 case inDrag:
  186.                     /* handled by hypercard */
  187.                     break;
  188.                     
  189.                 case inGrow:
  190.                 break;
  191.                     
  192.                 case inContent:
  193.                     if (whichWindow == FrontWindow() ){
  194.                         GetPort( &oldPort );
  195.                         SetPort( ip->eventWindow );
  196.                         
  197.                         hTE        = (TEHandle)GetWRefCon( ip->eventWindow );
  198.                         hit = ip->event.where;
  199.                         GlobalToLocal( &hit );
  200.                         bounds = (*hTE)->viewRect;
  201.                         if( PtInRect( hit, &bounds ) ){
  202.                             extend = (short)ip->event.modifiers && shiftKey;
  203.                             TEClick( hit, extend, hTE);
  204.                         }
  205.                         SetPort( oldPort );
  206.                     }else
  207.                         SelectWindow( whichWindow );
  208.  
  209.                     pp->passFlag = FALSE;
  210.                     break;
  211.                 
  212.                 default: 
  213.                     break;
  214.             }/* window part */
  215.         break;
  216.         
  217.     case mouseUp:
  218.         break;
  219.                 
  220.     case keyDown:
  221.     case autoKey:    
  222.         /* the command key will be handled by hypercard */
  223.         hTE        = (TEHandle)GetWRefCon( ip->eventWindow );
  224.         GetPort( &oldPort );
  225.         SetPort( ip->eventWindow );
  226.         theKey  = ip->event.message & 0xFF;
  227.         
  228.             switch( theKey ){
  229.                 case TAB:
  230.                 break;
  231.                 
  232.                 case ETX:
  233.                 break;
  234.                 
  235.                 case LEFT_ARROW:
  236.                 case RIGHT_ARROW:
  237.                 case UP_ARROW:
  238.                 case DOWN_ARROW:
  239.                 break;
  240.                 
  241.                 case BS:
  242.                 default:
  243.                     TEKey( theKey, hTE );
  244.                 break;
  245.             }/* switch( theKey ) */
  246.         SetPort( oldPort );
  247.         pp->passFlag = FALSE;
  248.         break;
  249.         
  250.     case activateEvt:
  251.         if ( ip->event.modifiers & activeFlag ){
  252.             BEGINXWEDIT( pp, ip->eventWindow );
  253.             hTE        = (TEHandle)GetWRefCon( ip->eventWindow );
  254.             TEActivate( hTE );
  255.         }
  256.         else{
  257.             ENDXWEDIT( pp, ip->eventWindow );
  258.             hTE        = (TEHandle)GetWRefCon( ip->eventWindow );
  259.             TEDeactivate( hTE );
  260.         }
  261.         break;
  262.         
  263.     case updateEvt: 
  264.         BeginUpdate( ip->eventWindow );
  265.         DrawGrowIcon( ip->eventWindow );
  266.         hTE        = (TEHandle)GetWRefCon( ip->eventWindow );
  267.         bounds = (*hTE)->viewRect;
  268.         TEUpdate( &bounds, hTE );
  269.         EndUpdate( ip->eventWindow );
  270.         break;
  271.         
  272.     case app4Evt:
  273.              {
  274.                  unsigned    char    *evtType = &(ip->event.message);
  275.                  
  276.                  switch( *evtType ){
  277.                      case MouseMovedEvt:
  278.                      break;
  279.                      
  280.                      case SuspendResumeEvt:
  281.                 break;
  282.                  }
  283.         }
  284.         break;
  285.  
  286.  
  287.     /****************************************/
  288.     /*         THE HYPERCARD EVENTS            */
  289.     /****************************************/
  290.  
  291.     
  292.     case xOpenEvt:
  293.         /* for illustrative purposes, we     */
  294.         /* add a text edit field to the     */
  295.         /* window                            */
  296.         SetPort( ip->eventWindow );
  297.         bounds = ip->eventWindow->portRect;
  298.         
  299.         bounds.top += 4;
  300.         bounds.left +=4;
  301.         bounds.bottom -= 16;
  302.         bounds.right -= 16;
  303.         hTE  = TENew( &bounds, &bounds );
  304.         (*hTE)->txFont = courier;
  305.         (*hTE)->txFace = 0;
  306.         (*hTE)->txSize = 10;
  307.         SetWRefCon( ip->eventWindow, (long)hTE );
  308.         ShowWindow( ip->eventWindow );
  309.         break;
  310.             
  311.     case xCloseEvt:
  312.         hTE        = (TEHandle)GetWRefCon( ip->eventWindow );
  313.         TEDispose( hTE );
  314.         HideWindow( ip->eventWindow );
  315.     break;
  316.     
  317.     case xGiveUpEditEvt:
  318.         hTE        = (TEHandle)GetWRefCon( ip->eventWindow );
  319.         TEDeactivate( hTE );
  320.     break;
  321.  
  322.     case xEditUndo:
  323.     break;
  324.         
  325.     case xEditCut:
  326.     break;
  327.         
  328.     case xEditCopy:
  329.     break;
  330.         
  331.     case xEditPaste:
  332.     break;
  333.         
  334.     case xEditClear:
  335.     break;
  336.     
  337.     default:
  338.         GetPort( &oldPort );
  339.         SetPort( ip->eventWindow );
  340.         GetMouse( &hit );
  341.         
  342.         hTE        = (TEHandle)GetWRefCon( ip->eventWindow );
  343.         bounds = (*hTE)->viewRect;
  344.         if( PtInRect( hit, &bounds ) ){
  345.             SetCursor( *GetCursor(iBeamCursor) );
  346.         }
  347.         else
  348.             InitCursor();
  349.         
  350.         TEIdle( hTE );
  351.         pp->passFlag = FALSE;
  352.         SetPort( oldPort );
  353.     }/* switch theEvent->what */
  354. }
  355.